//
//  ProgressView.m
//  Towerxi3
//
//  Created by 塔兮 on 2022/5/10.
//

#import "ProgressView.h"

@interface ProgressView ()
{
    CAShapeLayer* _progressLayer;
}
@end

@implementation ProgressView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self configureView];
    }
    return self;
}

- (UIView *)backView {
    if (!_backView) {
        _backView = [[UIView alloc]initWithFrame:CGRectMake((SCWidth - 230)/2, 240, 230, 230)];
        _backView.backgroundColor = UIColorFromHex(@"FFFFFF");
        [_backView setCornerRadius:10];
    }
    return _backView;
}

- (UIView *)circleView {
    if (!_circleView) {
        _circleView = [[UIView alloc]initWithFrame:CGRectMake((self.backView.width - 150)/2, 20, 150, 150)];
        _circleView.backgroundColor = UIColorFromHex(@"FFFFFF");
    }
    return _circleView;
}

- (UILabel *)label {
    if (!_label) {
        _label = [[UILabel alloc]initWithFrame:CGRectMake(0, self.circleView.bottom + 20, self.backView.width, 20)];
        _label.textColor = UIColorFromHex(@"2C2C2C");
        _label.font = [UIFont systemFontOfSize:14];
        _label.textAlignment = NSTextAlignmentCenter;
    }
    return _label;
}

- (UILabel *)progressNum {
    if (!_progressNum) {
        _progressNum = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 28)];
        _progressNum.textColor = UIColorFromHex(@"3080EE");
        _progressNum.font = [UIFont systemFontOfSize:20];
        _progressNum.center = self.circleView.center;
        _progressNum.textAlignment = NSTextAlignmentCenter;
    }
    return _progressNum;
}

- (void)configureView {
    [self addSubview:self.backView];
    [self.backView addSubview:self.circleView];
    [self.backView addSubview:self.label];
    [self.backView addSubview:self.progressNum];
    
    [self buildLayout];
}

- (void)buildLayout {
    float centerX = self.circleView.width/2.0;
    float centerY = self.circleView.height/2.0;
    //半径
    float radius = (self.circleView.width - 15)/2.0;
    
    //创建贝塞尔路径
    UIBezierPath *path = [UIBezierPath bezierPathWithArcCenter:CGPointMake(centerX, centerY) radius:radius startAngle:(-0.5f*M_PI) endAngle:1.5f*M_PI clockwise:YES];
    
    //添加背景圆环
    CAShapeLayer *backLayer = [CAShapeLayer layer];
    backLayer.frame = self.circleView.bounds;
    backLayer.fillColor =  [[UIColor clearColor] CGColor];
    backLayer.strokeColor  = UIColorFromHex(@"F1F1F1").CGColor;
    backLayer.lineWidth = 15;
    backLayer.path = [path CGPath];
    backLayer.strokeEnd = 1;
    [self.circleView.layer addSublayer:backLayer];
    
    //创建进度layer
    _progressLayer = [CAShapeLayer layer];
    _progressLayer.frame = self.circleView.bounds;
    _progressLayer.fillColor =  [[UIColor clearColor] CGColor];
    //指定path的渲染颜色
    _progressLayer.strokeColor  = UIColorFromHex(@"3080EE").CGColor;
    _progressLayer.lineCap = kCALineCapRound;
    _progressLayer.lineWidth = 15;
    _progressLayer.path = [path CGPath];
    _progressLayer.strokeEnd = 0;
    [self.circleView.layer addSublayer:_progressLayer];
}

- (void)setTitle:(NSString *)title {
    _title = title;
    self.label.text = title;
}

- (void)setProgress:(float)progress {
    _progress = progress;
    _progressLayer.strokeEnd = progress;
    [_progressLayer removeAllAnimations];
    
    self.progressNum.text = [NSString stringWithFormat:@"%.0f%@",progress * 100,@"%"];
}

@end
